package com.extra;

import java.util.*;

public class Solution1002 {
    public List<String> commonChars(String[] words) {
        HashMap<Character, Integer>[] maps = new HashMap[words.length];
        for (int i = 0; i < maps.length; i++) {
            maps[i] = new HashMap<>();
        }
        for (int j = 0; j < words.length; j++) {
            for (int i = 0; i < words[j].length(); i++) {
                maps[j].put(words[j].charAt(i), maps[j].getOrDefault(words[j].charAt(i), 0) + 1);
            }
        }
        for (char c : maps[0].keySet()) {
            for (int i = 1; i < maps.length; i++) {
                maps[0].put(c, Math.min(maps[0].get(c), maps[i].getOrDefault(c, 0)));
            }
        }
        List<String> res = new ArrayList<>();
        for (char c : maps[0].keySet()) {
            int num = maps[0].get(c);
            for (int i = 0; i < num; i++) {
                res.add(String.valueOf(c));
            }
        }
        return res;
    }
}
